//
//  File.swift
//
//
//  Created by didi on 2022/9/13.
//

import Vapor

public protocol ApiDeleteController: DeleteController {
    func deleteApi(_ req: Request) async throws -> HTTPStatus
    func setUpDeleteRoutes(_ routes: RoutesBuilder)
}

public extension ApiDeleteController {
    func deleteApi(_ req: Request) async throws -> HTTPStatus {
        let hasAccess = try await deleteAccess(req)
        guard hasAccess else { throw Abort(.forbidden) }

        let model = try await findBy(identifier(req), req)
        try await beforeDelete(req, model)
        try await model.delete(on: req.db)
        try await afterDelete(req, model)
        return .noContent
    }

    func setUpDeleteRoutes(_ routes: RoutesBuilder) {
        let baseRoutes = getBaseRoutes(routes)
        let existingModelRoutes = baseRoutes.grouped(ApiModel.pathIdComponent)
        existingModelRoutes.delete(use: deleteApi)
    }
}
